/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.ms1_intensitiesretrieval;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jonas
 */
public class IdentificationWindowUpdater {

    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(IdentificationWindowUpdater.class);

    public List compareFeaturesToIdentifications(List<MSLimsHits> identifications, List<FeatureVariables> features) {

        List<MSLimsHits> matchedIdentifications = new ArrayList<MSLimsHits>();

        for (MSLimsHits identification : identifications) {

            /* 
             * Smallest differences observed, in case there are other matches within the same window
             * Initiate these values on a slightly higher value than the tolerance level you set
             */
            double smallestTimeError = 30.0;
            double smallestMassError = 1.0;

            for (FeatureVariables feature : features) {
                double massError = Math.abs((feature.getPrecursorMZ() - identification.getPrecursorMZ()) / identification.getPrecursorMZ());
                double timeError = Math.abs(feature.getRetentionTime()-identification.getRetentionTime());
                
                /*
                 * This part decides if the feature that is currently being looked at, corresponds with the ms_limshit currently being looked at
                 * The tolerance on the massError is 0.6, what leaves room for the cases where the wrong isotopic mass is picked up by openMS.
                 * This happens sometimes and is not predictable.
                 * The tolerance on the timeError is 25.0 seconds, the same value is begin used as a tolerance parameter in the openMS featureFinder.
                 * In this case (and some others that were manually validated), 25 seconds is enough to pick up all the desired features.
                 * If, in another case, not all the features that you look for are found, increase this timevalue, but increase it in the openMS
                 * featurefinder aswell or you won't see any difference. Note that increasing this value also increases the chance for a wrong match to occur.
                 */
                
                if (massError < 0.6 && massError < smallestMassError){
                    if (timeError <= 25.0 && timeError < smallestTimeError){
                        smallestMassError = massError;
                        smallestTimeError = timeError;                        
                        identification.setStartPoint(feature.getStartPoint());
                        identification.setEndPoint(feature.getEndPoint());
                        identification.setOpenMSintensity(feature.getOpenMSIntensity());
                    }
                }
            }
            
            // The identification is only added to the new list, if a matching feature has been found in the featureFile
            if (identification.getStartPoint() != -1.0) {
                matchedIdentifications.add(identification);
            }
        }
        LOGGER.info("Feature windows mapped to ms_lims identifications!");

        return matchedIdentifications;
    }
}
